<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input placeholder="请输入内容" class="search-main" v-model="params.name"/>
      <el-button type="primary" @click="getList">查询</el-button>
    </div>
    <div>
      <el-button class="btn"  type="success" :round="true" @click="open">添加楼宇</el-button>
      <el-button type="info" :round="true" @click="delMore" >批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="list" 
      @selection-change="handleSelectionChange"
        style="width: 100%">
        <el-table-column type="selection" width="100"/> 
        
        <el-table-column
          type="index"
          label="序号"
        />
        <el-table-column
          label="楼宇名称"
          width="180" prop="name"
        />
        <el-table-column
          label="层数"
          prop="floors"
        />
        <el-table-column
          label="在管面积(m²)"
          width="120"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column
          label="状态"
          prop="status"
          :formatter="fn"
        />
        <el-table-column
          label="操作"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="edit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="delbuilding(scope.row.id,scope.row.status)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="box">
        <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.page"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
    </el-pagination>
  </div>
      
    </div>
    
    <el-dialog
        :title="form.id ? '编辑楼宇' : '添加楼宇'"
        :visible="flag"
        @close="close"
      >
        <div style="display: flex; justify-content: center;">
          <el-form ref="form" style="width: 400px;"  :model="form" :rules="rules">
          <el-form-item label="楼宇名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="楼宇层数" prop="floors">
          <el-input v-model="form.floors" />
        </el-form-item>
        <el-form-item label="在管面积" prop="area">
          <el-input v-model="form.area" />
        </el-form-item>
        <el-form-item label="物业费" prop="propertyFeePrice">
          <el-input v-model="form.propertyFeePrice" />
        </el-form-item>
       <el-form-item style="display: flex; justify-content: right;">
        <el-button type="info" plain @click="close">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
       </el-form-item>
        </el-form>
        </div>
        
        <!-- <template #footer>
          <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="dialogVisible = false">确 定</el-button>
        </template> -->
      </el-dialog>
     
  </div>
</template>

<script>
import { getBuildinglListAPI,addBuildinglListAPI,
  delBuildinglListAPI,editBuildinglListAPI } from '@/api/building';

export default {
  name: 'Building',
  data(){
    return{
      flag:false,
      list:[],
      total: 0,
      idStr: '',
      params: {
        page: 1,
        pageSize: 4
      },
      form:{
      name: '',
      floors: null,
      area: null,
      propertyFeePrice: null
      },
      rules:{
        name: [
        { required: true, message: '请输入楼宇名称', trigger: 'blur' }
      ],
      floors: [
        { required: true, message: '请输入楼宇层数', trigger: 'blur' }
      ],
      area: [
        { required: true, message: '请输入楼宇面积', trigger: 'blur' }
      ],
      propertyFeePrice: [
        { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
      ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods:{
    confirm(){
      this.$refs.form.validate((ok) => {
        if(ok){
         if(this.form.id){
          editBuildinglListAPI(this.form).then(() =>{
            this.getList()
            this.$message.success("编辑成功！")
            this.flag = false

            this.form = {
                  name: '',
                  floors: '',
                  area: '',
                  propertyFeePrice: ''
                  }
          })
         } else{
          this.addList()
        } 
        } 
      })
    },
    edit(row){
      this.flag=true
      this.form.name = row.name
      this.form.floors = row.floors
      this.form.area = row.area
      this.form.propertyFeePrice = row.propertyFeePrice
      this.form.id = row.id
    },
    handleSelectionChange(row){
      this.ele = row
    },
    delMore(){
      let temp = this.ele
      for(let index = 0;index < temp.length;index++){
        setTimeout(() => {
          delBuildinglListAPI(temp[index].id).then(() =>{
            this.getList()
          }).catch((error) =>{
            this.$message.error('删除失败!',error.response.data.msg)
          })
        }, 100);
      }
      this.$message.success('批量删除成功！')
      

      // console.log("你好
      
    },
    delbuilding(id,status){
      console.log(status)
     this.$confirm('确认要删除吗?').then(() =>{
      if(status === 1){
        this.$message.error('租赁中不能删除！')
      }else{
        delBuildinglListAPI(id).then(() => {
        this.getList()
        this.$message.success("删除成功！")
      }).catch(() => {
        this.$message.error("删除失败！")
      })
      }
    

     })
    },
    
    open(){
      this.flag=true
    },
    close(){
      this.flag=false
      this.form = {
          name: '',
          floors: '',
          area: '',
          propertyFeePrice: ''
          }
    },
    fn(row){
      return row.status === 0 ?'空置中' : '租赁中'
    },
    addList(){
      addBuildinglListAPI(this.form).then(() =>{
              this.$message.success('添加成功!')
              this.flag = false
              this.getList()
              this.form = {
                  name: '',
                  floors: '',
                  area: '',
                  propertyFeePrice: ''
                  }
      }).catch((error) => {
              // 
              console.dir(error);
              this.$message.error(error.response.data.msg)
            })
    },
    getList(){
      // console.log("hh",this.params);
      getBuildinglListAPI(this.params).then((res) => {
        this.list = res.data.rows
        this.total = res.data.total
        console.log(res);
      })
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(page){
      this.params.page = page
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.btn{
  margin: 15px;
}
.box{
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
  margin-right: 20px;
}
.department-container{
  padding:10px;
}
.search-container{
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237,237,237,.9);;
  padding-bottom: 20px;
  .search-label{
    width:100px;
  }
  .search-main{
    width: 220px;
    margin-right: 10px;
  }



}
</style>